package com.bitcar.common.hibernate;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import javax.sql.rowset.serial.SerialArray;
import javax.sql.rowset.serial.SerialBlob;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;


@Repository
public class HibernateDao {

	@Resource
	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

//	@SuppressWarnings("unchecked")
//	public Object get(Class cls, String id) {
//		return template.get(cls, id);
//	}
        
        @SuppressWarnings("unchecked")
        public Object get(Class cls,Serializable id){
            return template.get(cls, id);
        }

	@SuppressWarnings("unchecked")
	public Object uniqueResult(final String hql, final Object[] values) {
		List list = template.executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				for (int i = 0; i < values.length; i++) {
					query.setParameter(i, values[i]);
				}
				query.setMaxResults(1);
				return query.list();
			}
		});
		return (list == null || list.isEmpty()) ? null : list.get(0);
	}

	@SuppressWarnings("unchecked")
	public List list(final String hql, final Object[] values) {
		List list = template.executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				for (int i = 0; i < values.length; i++) {
					query.setParameter(i, values[i]);
				}
				return query.list();
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	public List list(final String hql, final Object[] values, final int offset,
			final int length) {
		List list = template.executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				for (int i = 0; i < values.length; i++) {
					query.setParameter(i, values[i]);
				}
				query.setFirstResult(offset);
				query.setMaxResults(length);
				return query.list();
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	public List listBySql(final String sql, final Object[] values,
			final int offset, final int length) {
		List list = template.executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sql);
				for (int i = 0; i < values.length; i++) {
					query.setParameter(i, values[i]);
				}
				query.setFirstResult(offset);
				query.setMaxResults(length);
				return query.list();
			}
		});
		return list;
	}


	public void save(Object obj) {
		template.save(obj);
	}

	public void update(Object obj) {
		template.update(obj);
	}

	public void saveOrUpdate(Object obj) {
		template.saveOrUpdate(obj);
	}

	public void delete(Object obj) {
		template.delete(obj);
	}
}
